﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GpxToolsLib;
using System.Windows.Controls.DataVisualization.Charting;
using Microsoft.Maps.MapControl;
using System.Collections.ObjectModel;
using TrailMap.Utils;

namespace TrailMap.Controls
{
    public partial class ElevationProfile : UserControl
    {
        public ElevationProfile()
        {
            InitializeComponent();
        }

        public void ClearPlot()
        {
            eleChart.Series.Clear();
            txtSummary.Text = "\r\n  Last Plot Summary:";
        }

        public void PlotTrack(List<trkpt> track)
        {
            List<PlotData> plotData = new List<PlotData>();

            Double lastDistance = 0.0;
            bool isFirstItem = true;
            double lastLat = 0;
            double lastLng = 0;


            for (int index = 0; index < track.Count; index ++)
            {
                trkpt item = track[index];
                if (isFirstItem)
                {
                    isFirstItem = false;
                    plotData.Add(new PlotData(item.Elevation, lastDistance));
                    lastLat = Double.Parse(item.lat);
                    lastLng = Double.Parse(item.lon);
                }
                else
                {
                    double currentLat = Double.Parse(item.lat);
                    double currentLng = Double.Parse(item.lon);

                    double currentDistance = lastDistance + DistanceUtil.Inst.Distance(lastLat, lastLng, currentLat, currentLng, 'K');

                    lastDistance = currentDistance;
                    lastLat = currentLat;
                    lastLng = currentLng;

                    plotData.Add(new PlotData(item.Elevation, currentDistance));
                }
            }

            
            AreaSeries areaseries = new AreaSeries();

            areaseries.DependentValuePath = "Dep";
            areaseries.IndependentValuePath = "Ind";
            areaseries.AnimationSequence = AnimationSequence.FirstToLast;
            areaseries.IsSelectionEnabled = true;
            areaseries.ItemsSource = plotData;
            lblXAxisUnit.Content = "in kilometers";
            //lblXAxisUnit.Content = String.Format("{0:F2} in kilometers", lastDistance);
            lblYAxisUnit.Content = "in meters";

            eleChart.Series.Add(areaseries);

            TimeSpan timeSpend = (track[track.Count - 1].time - track[0].time);

            string summaryText = string.Format("\r\n  Last Plot Summary:\r\n\tTotal Displacement : \r\n\t\t\t{0:F3}km\r\n\tTotal Time spend :\r\n\t\t\t{1} days {2} Hours",
                lastDistance, timeSpend.ToString("dd"), timeSpend.ToString(@"hh\:mm"));

            txtSummary.Text = summaryText;

        }

        private void bttnClear_Click(object sender, RoutedEventArgs e)
        {
            ClearPlot();
        }

        private void bttnHide_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

    public class PlotData
    {
        public PlotData(dynamic dep, dynamic ind)
        {
            this.Dep = dep;
            this.Ind = ind;
        }
        public dynamic Dep { get; set; }
        public dynamic Ind { get; set; }
    }
}
